package com.aaa.service.front.Impl;

import com.aaa.entity.Goods;
import com.aaa.entity.GoodsQuery;
import com.aaa.entity.PagerData;
import com.aaa.mapper.front.ZMRGoodsMapper;
import com.aaa.service.front.ZMRIGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ZMRGoodsServiceImpl implements ZMRIGoodsService {

    @Resource
    private ZMRGoodsMapper zmrGoodsMapper;

    @Override
    public List<Goods> LIST() {
        return zmrGoodsMapper.findAll();
    }

    @Override
    public List<Goods> LIST2(Map<String, Object> map) {
        return zmrGoodsMapper.findAll2(map);
    }

    @Override
    public PagerData<Goods> list(Integer categoryId, GoodsQuery goodsQuery, Integer curPage, Integer pageSize) {
        //构建分页查询所需要的参数
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("gq", goodsQuery);
        map.put("start", (curPage-1)*pageSize);
        map.put("pageSize", pageSize);

        //查询当前页的数据
        List<Goods> goodsList =  zmrGoodsMapper.findAll2(map);
        //查询总记录数
        int totalCount = zmrGoodsMapper.findTotalCount(map);
        //计算总页数
        int totalPage = totalCount%pageSize==0?
                totalCount/pageSize:
                totalCount/pageSize+1;

        //封装分页数据
        PagerData<Goods> pd = new PagerData<Goods>();
        pd.setCurPage(curPage);
        pd.setPageSize(pageSize);
        pd.setTotalCount(totalCount);
        pd.setTotalPage(totalPage);
        pd.setDataList(goodsList);

        return pd;
    }

    @Override
    public List<Goods> findById(Integer categoryId) {
        return zmrGoodsMapper.findById(categoryId);
    }

    /**
     * 查询商品详情
     * @param goodsId
     * @param
     * @return
     */
    @Override
    public List<Goods> findByGoodsId(String goodsId) {
        return zmrGoodsMapper.findByGoodsId(goodsId);
    }


    /**
     * 查询商品库存数量
     * @param goodsId
     * @return
     */
    @Override
    public Goods totalCount(String goodsId) {
        return zmrGoodsMapper.totalCount(goodsId);
    }


}
